package com.seo.controller.transaction;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.seo.function.PageTool;
import com.seo.pojo.ReturnBody;
import com.seo.service.sysUser.sdk.SysUserService;
import com.seo.service.user.recharge.sdk.RechargeService;
import com.seo.utils.ClientConst;
import com.seo.utils.CommonUtil;
import com.seo.utils.Jurisdiction;

import lombok.extern.slf4j.Slf4j;

/**
 * @ClassName: RechargeController
 * @author sbl
 * @version 2018.07.28 12:20
 */
@Slf4j
@Controller
@RequestMapping(value = "/recharge/")
public class RechargeController {

	@Autowired
	private RechargeService rechargeService;
	
	@Autowired
	private SysUserService sysUserService;
	
	@Autowired
	private PageTool pageTool;

	/**
	 * 充值明细
	 * 
	 * @param params
	 *            params
	 * @param request
	 *            request
	 * @return String
	 */
	@RequestMapping(value = "/list")
	public String list(@RequestParam Map<String, Object> params, HttpServletRequest request) throws Exception {
		String tradeId = CommonUtil.getUUID();
		log.info("[{}]-充值明细[{}]", tradeId, params);
		params.put("parentId", 0);
		Integer id = Jurisdiction.getT("id", Integer.class, ClientConst.SESSION_USER);
		Map<String, Object> sysUser = sysUserService.getSysUserById(id);
		String dataAuthority = (String)sysUser.get("dataAuthority");
		if("00".equals(dataAuthority)) {
			params.put("belongId", id);
		}
		ReturnBody rb = rechargeService.getRechargeDetailList(tradeId, params);

		if (rb.isSuccess()) {
			request.setAttribute("page", rb.get("page"));
			String userType = Jurisdiction.getT("userType", String.class);
			request.setAttribute("userType", userType);
			if ("01".equals(userType)) {
				Map<String, Object> p = new HashMap<>();
				List<?> sysUserList = pageTool.getListPage(sysUserService::getSysUserList, p, 1, 100);
				request.setAttribute("sysUserList", sysUserList);
			}
		}
		request.setAttribute("result", rb.getResult());
		return "bill/recharge_list";
	}

	@RequestMapping(value = "/add")
	public String add() {
		log.info("添加充值界面");
		return "bill/recharge_add";
	}

	@RequestMapping("/addRecharge")
	@ResponseBody
	public ReturnBody addRecharge(@RequestParam Map<String, Object> params, HttpServletRequest request) {

		String tradeId = CommonUtil.getUUID();
		log.info("[{}]-保存充值明细", tradeId);

		return rechargeService.saveArtificialRechargeDetail(tradeId, params);
	}

}
